package com.eptok.gw.szcups.cups.handler;

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ys8583.parse.IOMessage;

import com.eptok.gw.szcups.cups.message.PackMessageUtil;
import com.solab.iso8583.common.ByteUtil;

public class ActiveMerClientHandler extends IoHandlerAdapter{
	
	private static final Logger log = LoggerFactory.getLogger(PassiveMerClientHandler.class);

	private String handlerName;
	public ActiveMerClientHandler(String handlerName){
		this.handlerName = handlerName;
	}
	
	
	@Override
	public void exceptionCaught(IoSession session, Throwable cause)
			throws Exception {
		log.error(handlerName + " mina fale:",cause);
		super.exceptionCaught(session, cause);
		session.close(false);
	}

	@Override
	public void sessionIdle(IoSession session, IdleStatus status)
			throws Exception {
		//发送心跳
		session.write(PackMessageUtil.packHeartbeatMessage());
	}


	@Override
	public void messageSent(IoSession session, Object message) throws Exception {
		if(message instanceof IOMessage){
			IOMessage mes = (IOMessage)message;
			log.info(handlerName + "发送数据(不含长度):" + ByteUtil.BinaryToHexString(mes.toGetByte()));
			log.info(handlerName + "发送数据:" + mes.toGetBitmapStr());
		}
	}
	
	
}
